var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      msglist:[],
      now_page:1,
      totalpage:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.getmsg();
  },
  getmsg:function(){
    var that = this;
    var userid = wx.getStorageSync("userId");
    var param = {
      url: "api/user/msgList",
      data: {
        user_id: userid,
        now_page: 1,
      },
      success: function (e) {
        console.log(e)
        that.setData({
          msglist: e.data.data.list,
          totalpage:e.data.data.total_page
        })
      },
    };
    app.post(param);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.onLoad();
  },

  onReachBottom: function () {
    var that = this;
    
    // 页数+1
    var now_page = parseInt(that.data.now_page)+1;
    if (now_page > parseInt(that.data.totalpage)) {
      wx.showToast({
        title: '没有更多数据了',
        icon: "none",
        duration: 2000
      })
      return false;
    } else {
      // 显示加载图标
      wx.showLoading({
        title: '玩命加载中',
      })
    var userid = wx.getStorageSync("userId");
    var param = {
      url: "api/user/msgList",
      data: {
        user_id: userid,
        now_page: now_page
      },
      success: function (e) {
        that.setData({
          now_page: now_page,
          msglist:that.data.msglist.concat(e.data.data.list),
        })
        // 隐藏加载框
        wx.hideLoading();

      },
    };
    app.post(param);
    }
  },

  seegg:function(e){
    console.log(e)
    var id = e.currentTarget.dataset.id;
    var rid = e.currentTarget.dataset.rid;
    var type = e.currentTarget.dataset.type;
    this.yidu(id);
    if(parseInt(type) == 1 ){
        wx.navigateTo({
          url: '/pages/mine/ggdetail/ggdetail?id='+id,
        })
    } else if (parseInt(type) == 2){
      wx.navigateTo({
         url: '/pages/index/shangjia/shangjia?id=' + rid,
      })
    }else{
      wx.navigateTo({
        url: '/pages/mine/orderdetail/orderdetail?id=' + rid,
      })
    }
  },

  // 更新已读信息
  yidu:function(id){
    var param = {
      url:"api/common/hasRead",
      data:{
        msg_id: id
      },
      success:function(e){
        console.log(e)
      }
      
    };
    app.post(param);
  }
})